<?php
namespace SYSTEM;

class Languages {
    private $languagesFolderPath = './application/languages/';
    private $availableLangFolders = array();
    public $words = array();

    public function __construct() {
        //Todo safe in session this list
        $languages = scandir($this->languagesFolderPath);
        
        for($i = 2; $i < count($languages); $i++) {
            $this->availableLangFolders[] = $languages[$i];
        }
        
        if(!empty(\SYSTEM\ConfigsManager::$configs['languages']['autoload'])) {
            $this->set(\SYSTEM\ConfigsManager::$configs['languages']['autoload']);
        }
    }
    
    public function set($langCode) {
        if(in_array($langCode, $this->availableLangFolders)) {
            $this->loadLanguagesDir($this->languagesFolderPath.$langCode);
        } else {
            $this->loadLanguagesDir($this->languagesFolderPath.\SYSTEM\ConfigsManager::$configs['languages']['default']);
        }
    }
    
    private function loadLanguagesDir($dirPath) {
        $langFiles = scandir($dirPath);
        
        for($i = 2; $i < count($langFiles); $i++) {
            include realpath($dirPath.DIRECTORY_SEPARATOR.$langFiles[$i]);
        }
        
        $this->words = $lang;
    }
}